# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=libinih
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=60
pkgrel=1
pkgdesc="A simple .INI file parser written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/benhoyt/inih'
msys2_references=(
  'archlinux: libinih'
  'cygwin: inih'
  'gentoo: dev-libs/inih'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/benhoyt/inih/archive/r${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('706aa05c888b53bd170e5d8aa8f8a9d9ccf5449dfed262d5103d1f292af26774')

build() {
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/meson.exe setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      -Ddefault_library=static \
      -Ddistro_install=true \
      -Dwith_INIReader=true \
      "build-${MSYSTEM}-static" \
      "inih-r${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/meson.exe setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      -Ddefault_library=shared \
      -Ddistro_install=true \
      -Dwith_INIReader=true \
      "build-${MSYSTEM}-shared" \
      "inih-r${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-shared"
}

package() {
  ${MINGW_PREFIX}/bin/meson.exe install --destdir "${pkgdir}" -C "build-${MSYSTEM}-static"
  ${MINGW_PREFIX}/bin/meson.exe install --destdir "${pkgdir}" -C "build-${MSYSTEM}-shared"

  # https://github.com/benhoyt/inih/issues/162
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    echo "Cflags.private: -UINI_SHARED_LIB" >> ${_f}
  done

  install -Dm644 "${srcdir}/inih-r${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
